package chase.myrewards;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.ObjectInputStream;
import org.apache.http.HttpEntity;

import chase.myrewards.beans.Customer;

/**
 * Activity used to unlock the app. It contains a text field in which the user 
 * types a password.  If the password is correct, it will take the user to the
 * account login activity.
 * 
 * @author Meena Abdou
 * 
 */
public class AppLoginActivity extends Activity {

	private TextView incorrectPassword;
	private EditText appPassword;
	private TextView registerScreen;
	private Button loginBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.applogin);

		incorrectPassword = (TextView) findViewById(R.id.incorrect_password);
		appPassword = (EditText) findViewById(R.id.app_password);  
		registerScreen = (TextView) findViewById(R.id.link_to_register);
		loginBtn = (Button) findViewById(R.id.btnLogin);

		registerScreen.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Switching to Register screen
				Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
				startActivity(i);
			}
		});

		loginBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				new UnlockTask().execute();
			}
		});
	}

	private void unlock(){		
		// Reading the entered password value
		String password = appPassword.getText().toString();
		Log.d("", "password is " + password);

		try {
			String url = "http://my-rewards.appspot.com/applogin";
			//String url = "http://192.168.1.4:8888/applogin";

			MyRewardsClient.getInstance().setUrl(url);
			MyRewardsClient.getInstance().AddParam("password", password);

			Log.d("", "sending request to server");

			MyRewardsClient.getInstance().Execute(RequestMethod.GET, false);  // this is a bean request
			HttpEntity entity = MyRewardsClient.getInstance().getResponseEntity();

			Log.d("", "getting response back from server");

			// Get the customer bean sent from the server
			ObjectInputStream in = new ObjectInputStream(entity.getContent());
			Customer c = (Customer) in.readObject();
			Log.d("", "customer is " + c);
			in.close();

			if (c != null){
				MyRewardsClient.getInstance().setLoggedIn(true);
				MyRewardsClient.getInstance().setCustomer((Customer) c);
			}
		}
		catch (Exception ex) {
			Log.e("", "Exception", ex);
		}

		Log.d("AppLogin", "Logged in:" + MyRewardsClient.getInstance().isLoggedIn());
	}

	private class UnlockTask extends AsyncTask<Integer, Integer, Boolean> {
		ProgressDialog progressDialog;

		@Override
		protected void onPreExecute(){
			progressDialog = ProgressDialog.show(AppLoginActivity.this, "Unlocking", "Please Wait", true);
		}

		@Override
		protected Boolean doInBackground(Integer... params) {
			unlock();
			return true;
		}

		@Override
		protected void onPostExecute(Boolean result){
			progressDialog.dismiss();
			if(MyRewardsClient.getInstance().isLoggedIn() == true){
				Intent i = new Intent(getApplicationContext(), AccountLoginActivity.class);
				startActivity(i);
				finish();
			}
			else{
				incorrectPassword.setVisibility(View.VISIBLE);
			}
		}
	}
}
